{
  This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
  the Free Software Foundation; version 2 of the License.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
}

// Copyright (c) 2010 2011 2012 - J. Aldo G. de Freitas Junior

{$mode objfpc}
{$H+}

Program
	XMLRegistryConsole;

Uses
	Classes,
	SysUtils,
	SimpleIPC;

Var
	gFocused,
	gCommandLine,
	gMyRandomName,
	gPrompt : String;
	gIPCServer : TSimpleIPCServer;

Procedure SendCommand;
Var
	lIPCClient : TSimpleIPCClient;
	lLine : TStringStream;
Begin
	lLine := TStringStream.Create('');
	lIPCClient := TSimpleIPCClient.Create(Nil);
	Try
		Try
			lIPCClient.ServerID := 'xmlregistry';
			lIPCClient.Connect;
			lLine.WriteAnsiString(gMyRandomName);
			lLine.WriteAnsiString(gFocused);
			lLine.WriteAnsiString(gCommandLine);
			lIPCClient.SendStringMessage(lLine.DataString);
		Finally
			FreeAndNil(lLine);
			FreeAndNil(lIPCClient);
		End;
	Except
		On E: Exception Do
		Begin
			WriteLn('Local error : ', E.Message);
			Halt(1);
		End;
	End;
End;

Procedure PrintLine;
Var
	lLine : TStringStream;
	lOut : String;
	lMode : Byte;
	lEditFile : String;
	lEditor : String;
	lResultEditCommand : String;
	lPrompt : String;
	lFocused : String;
Begin
	Try
		Try
			lLine := TStringStream.Create(gIPCServer.StringMessage);
			lMode := lLine.ReadByte;
			Case lMode Of
				00 :
				Begin
					gPrompt := lLine.ReadAnsiString;
					gFocused := lLine.ReadAnsiString;
					lOut := lLine.ReadAnsiString;
					Write(lOut);
				End;
				01 : 
				Begin
					lPrompt := lLine.ReadAnsiString;
					lFocused := lLine.ReadAnsiString;
					lOut := lLine.ReadAnsiString;
					lEditFile := lLine.ReadAnsiString;
					lEditor := lLine.ReadAnsiString;
					lResultEditCommand := lLine.ReadAnsiString;
					ExecuteProcess('/bin/bash', [ '-c', lEditor, lEditFile ]);
					gCommandLine := lResultEditCommand;
					SendCommand;
					gCommandLine := '';
				End;
			End;
		Finally
			FreeAndNil(lLine);
		End;
	Except
		On E: Exception Do
			WriteLn('Local error : ', E.Message);
	End;
End;

Begin
	gMyRandomName := 'xmlregistryconsole_' + FloatToStr(Random);
	gFocused := '$root$';
	gPrompt := '';
	gCommandLine := '';
	gIPCServer := TSimpleIPCServer.Create(Nil);
	gIPCServer.ServerID := gMyRandomName;
	gIPCServer.Global := True;
	gIPCServer.StartServer;
	Try
		Repeat
			Write(gPrompt, '>'); ReadLn(gCommandLine);
			If (gCommandLine <> '') And (gCommandLine <> 'quit') Then
			Begin
				SendCommand;
				While Not gIPCServer.PeekMessage(1, False) Do
					Sleep(1);
				While gIPCServer.PeekMessage(1, True) Do
					PrintLine;
			End;
		Until LowerCase(gCommandLine) = 'quit';
	Finally
		gIPCServer.StopServer;
		FreeAndNil(gIPCServer);
	End;
End.